package Homework07;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import Homework08.AgeException;

public class Homework06 {
	public static void main(String[] args) throws Exception {
		String path = "names.txt";
		String path2="names2.txt";
		String pathSource = "name1.txt";
		String pathCopy = "name2.txt";
		//int f = 1;
		int f = frequency();
		//System.out.println(f);
		File file = new File(path2);
		if(file.length() == 0||!file.exists()){
			PZ.mingzi();
			copy(path, path2);
		}
		if(f==1){
			FileWriter writer = new FileWriter(pathSource);
			ArrayList<Student> students1 = new ArrayList<>();
			addStudentName(students1, path2);
			System.out.println(students1);
			BufferedWriter br = new BufferedWriter(new FileWriter(pathSource));
			for(int i=0;i<10;i++){
				int luckyIndex1 = randomStudentIndex(students1);
				br.write(students1.get(luckyIndex1).toString()+"\n");
				System.out.println(students1.get(luckyIndex1).toString());
				delete(path2, students1.get(luckyIndex1));
				br.flush();
				students1.remove(luckyIndex1);
			}
			br.close();
			copy(pathSource,pathCopy);
		}
		List<Student> students = new ArrayList<>();
		addStudentName(students,"name2.txt");
		int luckyIndex = randomStudentIndex(students);
		System.out.println(students.get(luckyIndex).getName());
		//System.out.println(students.get(luckyIndex));
		delete(pathCopy,students.get(luckyIndex));
		
		
	}
	
	private static void delete(String path, Student student) throws IOException {
	    String msg = student.getName() + '-' + student.getGender() + '-' + student.getAge() + '-' + student.getWeight();
	    
	    // Read the entire file into a StringBuilder
	    BufferedReader br = new BufferedReader(new FileReader(path));
	    StringBuilder sb = new StringBuilder();
	    String line;
	    
	    while ((line = br.readLine()) != null) {
	        sb.append(line).append("\n");
	    }
	    
	    br.close();
	    
	    // Remove all occurrences of the student's information from the StringBuilder
	    int index;
	    while ((index = sb.indexOf(msg)) != -1) {
	        int endIndex = sb.indexOf("\n", index);
	        sb.delete(index, endIndex != -1 ? endIndex + 1 : sb.length());
	    }
	    
	    // Write the modified StringBuilder back to the file
	    BufferedWriter writer = new BufferedWriter(new FileWriter(path));
	    writer.write(sb.toString());
	    writer.close();
	}


	private static int randomStudentIndex(List<Student> students) {
		Random rand = new Random();
		int index = rand.nextInt(students.size());
		return index;
	}

	private static void addStudentName(List<Student> students,String path) throws AgeException, IOException {
		BufferedReader br = new BufferedReader(new FileReader(path));

		String line = null;

		while ((line = br.readLine()) != null) {
			Student s = new Student();
			String[] lines = line.split("-");
			if (lines.length == 4) {
				s.setName(lines[0]);
				s.setGender(lines[1]);
				s.setAge(Integer.parseInt(lines[2]));
				s.setWeight(Double.parseDouble(lines[3]));
				students.add(s);
			}
		}
		br.close();

	}
	
	private static void copy(String pathSource, String pathCopy) throws IOException {
		BufferedReader br = new BufferedReader(new FileReader(pathSource));
		BufferedWriter bw = new BufferedWriter(new FileWriter(pathCopy));
		
		String line=null;
		while((line=br.readLine())!=null){
			
			bw.write(line);
			bw.newLine();
			
		}
		br.close();
		bw.close();
		
	}

	private static int frequency() {
		String path = "frequency2.txt";
		int i = 1;

		try {
			File file = new File(path);

			if (file.exists()) {
				try (BufferedReader br = new BufferedReader(new FileReader(path))) {
					String line = br.readLine();
					if (line != null) {
						i = Integer.parseInt(line) + 1;
					}
				}
			}

			try (BufferedWriter bw = new BufferedWriter(new FileWriter(path))) {
				if(i >= 11){
					i=1;
				}
				bw.write(String.valueOf(i));
				bw.newLine();
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		return i;
	}

}
